//JavaScript代码区域
layui.use(['layer', 'form', 'laydate', 'table'], function() {
	var layer = layui.layer,
		$ = layui.$,
		form = layui.form,
		laydate = layui.laydate,
		table = layui.table;
	//监听提交
	form.on('submit(formDemo)', function(data) {
		table.reload('idTest', {
			where: data.field
		});
		return false;
	});
	//执行一个laydate实例
	laydate.render({
		elem: '#start',
		max: 0
	});
	laydate.render({
		elem: '#end',
		max: 0
	});

	//数据表格实例
	table.render({
		id: 'idTest',
		elem: '#demo',
		url: IP + '/api/teacher_eav/evaManager',
		height: 'full-120',
		method: 'get',
		page: true,
		even: true,
		cols: [
			[{
					type: 'checkbox'
				},
				{
					field: 'wtId',
					width: 100,
					title: '老师id',
					sort: true,
					hide: true
				}, {
					field: 'wtNumber',
					title: '测评批次号',
					width: 100
				}, {
					field: 'wtBegin',
					title: '开始时间',
					style: 'text-align:center;'
				}, {
					field: 'wtEnd',
					title: '结束时间',
					style: 'text-align:center;'
				}, {
					field: 'wtDescribe',
					title: '说明',
					style: 'text-align:center;',
					totalRow: true,
				}, {
					field: 'wtState',
					title: '状态',
					style: 'text-align:center;',
					totalRow: true
				}, {
					width: 200,
					title: '操作',
					align: 'center',
					toolbar: '#barDemo'
				}
			]
		],

		request: {
			pageName: 'page',
			limitName: 'pageSize'
		},
		response: {
			statusName: 'code',
			statusCode: 200,
			msgName: 'message',
			countName: 'count',
			dataName: 'data'
		},
		done: function(res, curr, count) {
			layer.closeAll('loading');
			$('tr:first').css({
				'background-color': '#4BB2FF',
				'color': '#fff',
				'font-size': '80px'
			});
		},
		parseData: function(res) { //res 即为原始返回的数据
			return parseResultData(res);
		}
	});
	// 编辑状态
	function parseResultData(res) {
		var data = res.data;
		var paseData = [];
		$.each(data, function(idx, obj) {
			var wtId = obj.wtId;
			var wtNumber = obj.wtNumber;
			if(obj.wtState == 1) {
				obj.wtState = '<input wtId="' + wtId + '" wtNumber="' + wtNumber + '" lay-filter="jobcheckbox" type="checkbox" lay-skin="switch" lay-text="已启用|未启用" checked>'
			} else if(obj.wtState == 0) {
				obj.wtState = '<input wtId="' + wtId + '" wtNumber="' + wtNumber + '" lay-filter="jobcheckbox" type="checkbox" lay-skin="switch" lay-text="已启用|未启用">'
			}
			paseData.push(obj)
		});
		return {
			"code": res.code,
			"msg": res.message,
			"count": res.count,
			"data": paseData
		};
	}

	//数据表格监听工具条(查看、编辑、删除按钮)
	table.on('tool(test)', function(obj) {
		var data = obj.data;
		var layEvent = obj.event;
		if(layEvent === 'edit') {
			layer.open({
				type: 2,
				title: '修改测评任务',
				shadeClose: false,
				shade: 0.8,
				area: ['95%', '80%'],
				content: 'task_update.html?wtId=' + data.wtId + ''
			});
		} else if(layEvent === "del") {
			var wN = data.wtNumber;
			var wtId = data.wtId;
			deleteEvaManager(wN, wtId);
		}
	});

	//复选框批量操作
	table.on('checkbox(test)', function(obj) {
		var checkStatus = table.checkStatus('idTest');
		if(checkStatus.data.length > 0) {
			$('#deleteall').show()
		} else {
			$('#deleteall').hide()
		}
	});

	//添加按钮事件
	$('#addEvaManager').on('click', function() {
		window.location = 'task_add.html'
	})
	//批量删除按钮
	$('#deleteall').click(function() {
		var checkStatus = table.checkStatus('idTest');
		if(checkStatus.data.length > 0) {
			var list = checkStatus.data;
			var wtId = [];
			var wtN = "";
			$.each(list, function(idx, obj) {
				wtId.push(obj.wtId)
				wtN += " " + obj.wtNumber
			});
			//			deleteEvaManager(wtN,wtId);
		}
	})

	//测评任务状态开关
	form.on('switch(jobcheckbox)', function(data) {
		var obj = data.elem;
		var wtId = obj.getAttribute("wtId");
		var wtNumber = obj.getAttribute("wtNumber");
		var wtState = obj.checked ? 'true' : 'false';
		var _data = JSON.stringify({
			wtId: wtId,
			wtState: wtState,
			wtNumber: wtNumber
		})
		obj.setAttribute('disabled', 'disabled');
		form.render();
		updateEvaManagerState(_data, obj);
	});
	// 修改状态
	function updateEvaManagerState(data, obj) {
		$.ajax({
			url: IP + '/api/teacher_eav/evaManager/myState',
			type: 'PUT',
			data: data,
			dataType: 'json',
			contentType: "application/json; charset=utf-8",
			success: function(result, status, xhr) {
				layer.closeAll('loading');
				if(result.code == 400) {
					obj.checked = false
					layer.msg(result.data, {
						icon: 2,
						time: 1000
					}, function() {
						obj.removeAttribute('disabled', 'disabled');
						form.render();
					});
					form.render();
				} else {
					layer.msg(result.data, {
						icon: 1,
						time: 1000
					}, function() {
						obj.removeAttribute('disabled', 'disabled');
						form.render();
					});
				}
			},
			error: function(xhr, textStatus, errorThrown) {
				layer.closeAll('loading');
				var status = xhr.status; // http status
				var msg = xhr.responseText;
				var message = "";
				if(msg != undefined && msg != "") {
					console.log(msg)
					var response = JSON.parse(msg);
					var exception = response.message;
					if(exception) {
						message = exception;
					} else {
						message = response.message;
					}
				}
				var flag = typeof(layer) == "undefined";
				if(status == 400) {
					if(flag) {
						alert(message);
					} else {
						layer.msg(message, {icon: 2,time: 1000});
					}
				} else if(status == 401) {
					console.log('access_token过期');
					sessionStorage.clear();
					localStorage.clear();
					location.href = "login3.html";
				} else if(status == 403) {
					obj.checked = false;
					obj.removeAttribute('disabled', 'disabled');
					form.render();
					message = "未授权";
					if(flag) {
						alert(message);
					} else {
						layer.msg(message, {icon: 4,time: 1000});
					}
				} else if(status == 404) {
					message = "路径未找到";
					if(flag) {
						alert(message);
					} else {
						layer.msg(message, {icon: 2,time: 1000});
					}
				} else if(status == 500) {
					message = '系统错误：' + message + '，请刷新页面，或者联系管理员';
					if(flag) {
						alert(message);
					} else {
						layer.msg(message, {icon: 2,time: 1000});
					}
				}
			}
		});
	}
	// 删除活动
	function deleteEvaManager(wN, wtId) {
		layer.confirm('确认删除编号:' + wN + " ？", {
			icon: 3
		}, function(index) {
			layer.close(index);
			$.ajax({
				url: IP + '/api/teacher_eav/evaManager/' + wtId,
				type: "DELETE",
				success: function(result, status, xhr) {
					layer.closeAll('loading');
					layer.msg(result.message, {
						icon: 1,
						time: 1000
					}, function() {
						table.reload('idTest', {});
					});
				}
			});
		});
	}

});